import http from 'http';
import winston from 'winston';

import app from './koa.js';
import { attachWebSocket } from './websocket.js';
import { listenAsync } from '../common/utils/promise-utils.js';

// Env settings
import env from '../../../conf.js';

export async function start() {
    const httpServer = http.createServer(app.callback());

    // 启用 Websocket
    attachWebSocket(httpServer);

    // 配置日志级别, 仅在开发环境时会显示调试内容
    if (process.env.NODE_ENV === 'development') {
        winston.remove(winston.transports.Console);
        winston.add(winston.transports.Console, {
            level: 'debug',
            colorize: true,
            timestamp: true,
            prettyPrint: true,
            humanReadableUnhandledException: true
        });
    } else {
        winston.level = 'info';
    }

    return listenAsync(httpServer, env.API_MOCK_PORT)
        .then(() => {
            winston.info('Mock Server Started', {
                url: `http://localhost:${env.API_MOCK_PORT}/`
            });
        });
}
